<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>总账凭证</title>
	<include file="Public:header" />
	<style type="text/css">
		.input{width:180px; border:1px solid #ccc; padding:2px;}
		.input0{border:0px; padding:0px; margin:0px; height:14px; overflow:hidden;}
		.input2{border:1px solid #ccc; padding:0px; margin:1px 0px 1px 0px; height:20px; display:inline-block;}
		.select{width:180px; border:1px solid #ccc; height:25px; padding:2px;}
	</style>
	<script type="text/javascript">		
		var actflg="{:C('URL_MODEL')}"=="0"?"&":"?";
		function chkfrm(act)
		{
			if($('#remark').val()=='')
			{
				$.messager.show({title:'提示',msg:'摘要不能为空',timeout:5000,showType:'slide'});
				return false;
			}
			if($('#dr_total').val()<=0)
			{
				$.messager.show({title:'提示',msg:'借方总计金额不能为0',timeout:5000,showType:'slide'});
				return false;
			}
			if($('#cr_total').val()<=0)
			{
				$.messager.show({title:'提示',msg:'贷方总计金额不能为0',timeout:5000,showType:'slide'});
				return false;
			}
			if($('#dr_total').val()!=$('#cr_total').val())
			{
				$.messager.show({title:'提示',msg:'借贷不平衡',timeout:5000,showType:'slide'});
				return false;
			}
			if(act=='post')
			{
				$('#form1').attr('action',"{:U('Finance/vou_post')}");
			}
			else if(act=='save-post')
			{
				$('#form1').attr('action',"{:U('Finance/vou_save_post')}");
			}
			$('#form1').submit();

		}
		
		function open_facc_window(itemno)
		{
			open_popup_faccmas('objcode=vouitem['+itemno+'][acc_code]&objtitle=vouitem['+itemno+'][acc_title]');
		}
		function open_curr_window(itemno)
		{
			open_popup_currmas('objcode=vouitem['+itemno+'][curr_code]&objrate=vouitem['+itemno+'][curr_rate]&callback=parent.calc('+itemno+');');
		}
		function open_supplier_window(itemno)
		{
			open_popup_supplier('objid=vouitem['+itemno+'][supplier_id]&objtitle=vouitem['+itemno+'][supplier_title]');
		}
		function open_customer_window(itemno)
		{
			open_popup_customer('objid=vouitem['+itemno+'][customer_id]&objtitle=vouitem['+itemno+'][customer_title]');
		}
		function calc(itemno)
		{
			if($("input[id='vouitem["+itemno+"][facc_id]']").val()=="")
			{
				$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][dr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][cr]']").attr("value","");
				return false;
			}
			var v_curr_rate = $("input[id='vouitem["+itemno+"][curr_rate]']").val();
			var v_curr_dr = $("input[id='vouitem["+itemno+"][curr_dr]']").val();
			var v_curr_cr = $("input[id='vouitem["+itemno+"][curr_cr]']").val();
			if(isNaN(v_curr_dr))
			{
				$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][dr]']").attr("value","");
				v_curr_dr = "";
			}
			if(isNaN(v_curr_cr))
			{
				$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][cr]']").attr("value","");
				v_curr_cr = "";
			}
			if(v_curr_dr!="" && v_curr_cr!="")
			{
				//$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value","");
				//$("input[id='vouitem["+itemno+"][dr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][cr]']").attr("value","");
				v_curr_cr = "";
				$.messager.show({title:'提示',msg:'借贷金额不能同时输入在同一条分录上',timeout:5000,showType:'slide'});
				//return false;
			}
			//alert("[" + v_curr_dr + "] [" + v_curr_cr + "]");
			if(v_curr_dr==""){
				$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][dr]']").attr("value","");
			}else{
				//alert("row total="+v_curr_dr*v_curr_rate);
				$("input[id='vouitem["+itemno+"][dr]']").attr("value",v_curr_dr*v_curr_rate);
			}
			if(v_curr_cr==""){
				$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value","");
				$("input[id='vouitem["+itemno+"][cr]']").attr("value","");
			}else{
				$("input[id='vouitem["+itemno+"][cr]']").attr("value",v_curr_cr*v_curr_rate);
			}
			calc_total();
		}
		function calc_total()
		{
			var v_item_num = $("#item_num").val();
			var v_total_dr = 0;
			var v_total_cr = 0;
			
			for(itemp=0;itemp<v_item_num;itemp++)
			{
				if($('#datarow'+itemp).length<=0) continue;
				v_total_dr += $("input[id='vouitem["+itemp+"][dr]']").val()=="" ? 0 : parseFloat($("input[id='vouitem["+itemp+"][dr]']").val());
				v_total_cr += $("input[id='vouitem["+itemp+"][cr]']").val()=="" ? 0 : parseFloat($("input[id='vouitem["+itemp+"][cr]']").val());
			}
			$("#dr_total").attr("value",v_total_dr.toFixed(2));
			$("#cr_total").attr("value",v_total_cr.toFixed(2));
		}
		function addRow(rownum)
		{
			var row_start = parseInt($("#item_num").val());
			var row_end = row_start + rownum;
			
			for(itemp=row_start;itemp<row_end;itemp++)
			{
				var list_html = '<div id="datarow'+itemp+'" class="list-body">	<div class="list-t-l w150px">';
				list_html += '	<input type="text" id="vouitem['+itemp+'][acc_code]" name="vouitem['+itemp+'][acc_code]" style="display:none;" />';
				list_html += '	<span class="searchbox input2" >';
				list_html += '	<input id="vouitem['+itemp+'][acc_title]" name="vouitem['+itemp+'][acc_title]" readonly="true" type="text" class="input0" style="width:130px; " />';
				list_html += '	<span><span class="searchbox-button" onclick="open_facc_window('+itemp+')"></span></span></span>';
				list_html += '</div>';
				list_html += '<div class="list-t-l w80px">';
				list_html += '	<input type="text" id="vouitem['+itemp+'][customer_id]" name="vouitem['+itemp+'][customer_id]" style="display:none;" />';
				list_html += '	<span class="searchbox input2" >';
				list_html += '	<input id="vouitem['+itemp+'][customer_title]" name="vouitem['+itemp+'][customer_title]" readonly="true" type="text" class="input0" style="width:60px; " />';
				list_html += '	<span><span class="searchbox-button" onclick="open_customer_window('+itemp+')"></span></span></span>';
				list_html += '</div>';
				list_html += '<div class="list-t-l w80px">';
				list_html += '	<input type="text" id="vouitem['+itemp+'][supplier_id]" name="vouitem['+itemp+'][supplier_id]" style="display:none;" />';
				list_html += '	<span class="searchbox input2" >';
				list_html += '	<input id="vouitem['+itemp+'][supplier_title]" name="vouitem['+itemp+'][supplier_title]" readonly="true" type="text" class="input0" style="width:60px; " />';
				list_html += '	<span><span class="searchbox-button" onclick="open_supplier_window('+itemp+')"></span></span></span>';
				list_html += '</div>';
				list_html += '<div class="list-t-l w80px">';
				list_html += '	<span class="searchbox input2" >';
				list_html += '	<input id="vouitem['+itemp+'][curr_code]" name="vouitem['+itemp+'][curr_code]" value="{$currmas.code}" readonly="true" type="text" class="input0" style="width: 50px; ">';
				list_html += '	<span><span class="searchbox-button" onclick="open_curr_window('+itemp+')"></span></span></span>';
				list_html += '</div>';
				list_html += '<div class="list-t-l w60px"><input type="text" onchange="calc('+itemp+')" id="vouitem['+itemp+'][curr_rate]" name="vouitem['+itemp+'][curr_rate]" class="input2" value="{$currmas.rate}" readonly="true" style="width:60px;" /></div>';
				list_html += '<div class="list-t-l w80px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'dr\','+itemp+')" id="vouitem['+itemp+'][curr_dr]" name="vouitem['+itemp+'][curr_dr]" class="input2" style="width:80px;" /></div>';
				list_html += '<div class="list-t-l w80px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'dr\','+itemp+')" id="vouitem['+itemp+'][dr]" name="vouitem['+itemp+'][dr]" class="input2" style="width:80px;"  /></div>';
				list_html += '<div class="list-t-l w80px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'cr\','+itemp+')" id="vouitem['+itemp+'][curr_cr]" name="vouitem['+itemp+'][curr_cr]" class="input2" style="width:80px;" /></div>';
				list_html += '<div class="list-t-l w80px"><input type="text" onchange="calc('+itemp+')" onkeyup="chk_amount_input(\'cr\','+itemp+')" id="vouitem['+itemp+'][cr]" name="vouitem['+itemp+'][cr]" class="input2" style="width:77px;"  /></div>';
				<if condition="$mydata['status_flg'] eq 'A'">
				list_html += '<div class="list-t-l w60px"><a href="javascript:;" onclick="datarow_remove('+itemp+')">删除行'+(itemp+1)+'</a></div>';
				</if>
				list_html += '</div>';
				$("#vou_list_body").append(list_html);
			}
			$("#item_num").attr("value",row_end);
		}
		function chk_amount_input(flg,itemno)
		{
			var v_amt = $("input[id='vouitem["+itemno+"][curr_"+flg+"]']").val();
			if(v_amt.indexOf(' ')>=0 && !isNaN(v_amt))
			{
				var v_curr_rate = $("input[id='vouitem["+itemno+"][curr_rate]']").val();
				switch(flg)
				{
					case 'dr':
						$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value","");
						$("input[id='vouitem["+itemno+"][dr]']").attr("value","");
						$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value",v_amt.replace(' ',''));
						$("input[id='vouitem["+itemno+"][cr]']").attr("value",v_amt*v_curr_rate);
						break;
					case 'cr':
						$("input[id='vouitem["+itemno+"][curr_cr]']").attr("value","");
						$("input[id='vouitem["+itemno+"][cr]']").attr("value","");
						$("input[id='vouitem["+itemno+"][curr_dr]']").attr("value",v_amt.replace(' ',''));
						$("input[id='vouitem["+itemno+"][dr]']").attr("value",v_amt*v_curr_rate);
						break;
				}
				calc_total();
			}
		}
		function datarow_remove(itemno)
		{
			$.messager.confirm('询问', '确认要移除选中的行吗？', function(r){
				if (r){
						$('#datarow'+itemno).remove();
						calc_total();
					}
				});
		}
		function cancel_todo()
		{
			$.messager.confirm('询问', '确认要取消当前凭证吗？', function(r){
					if (r){
						window.location.href="{:U('Finance/vou_status_to_c?id='.$mydata['id'].'')}";
					}
			});
		}
		function writeoff_todo()
		{
			var dt1 = $('#fadate').datebox('getValue');
			var dt2 = $('#fadate_write_off').datebox('getValue');
			if(new Date(dt2)<new Date(dt1))
			{
				$.messager.show({title:'提示',msg:'红字凭证的日期不能早于原始凭证的日期',timeout:5000,showType:'slide'});
				return false;
			}
			$.messager.confirm('询问', '确认要红字冲销当前凭证到【'+dt2+'】吗？', function(r){
					if (r){
						window.location.href="{:U('Finance/vou_write_off')}"+actflg+"id={$mydata.id}&fadate="+dt2+"";
					}
			});
		}
		
		window.onload=function()
		{
			
		}
	</script>
</head>
<body class="">

	<div class="demo-info space-out-10px">
		<div class="demo-tip icon-tip"></div>
		<div>总账凭证<if condition="$mydata['status_flg'] eq 'A'"> ：(1)点击放大镜可以选择所需的内容；(2)在金额输入框里按空格键，可以切换金额的借贷方向</if></div>
	</div>
	<form id="form1" method="post" action="{:U('Finance/vou_edit_save')}">
	<div class="space-out-10px">
		<div style="width:100%; height:25px;">
				<div class="list-t-l w60px">凭证号码：</div>
				<div class="list-t-l w200px"><input type="text" id="vouno" name="vouno" class="input" value="{$mydata.vouno}" readonly="true" /></div>
				<div class="list-t-l w100px" align="right">日期：</div>
				<div class="list-t-l w200px"><input type="text" id="fadate" name="fadate" class="easyui-datebox input" editable="false" value="{$mydata.fadate|date='Y-m-d',###}" /></div>
		</div>
		<div style="height:3px; clear:both;"></div>
		<div style="width:100%; height:25px;">
				<div class="list-t-l w60px">凭证状态：</div>
				<div class="list-t-l w200px"><input type="hidden" id="status_flg" name="status_flg" value="{$mydata.status_flg}" /><input type="text" name="tmp_title" class="input" value="{$mydata.status_flg|get_status_title=###}" readonly="true" /></div>
				<div class="list-t-l w100px" align="right">摘要：</div>
				<div class="list-t-l w300px"><input type="text" id="remark" name="remark" class="" style="width:280px; border:1px solid #ccc; padding:2px;" required="true" value="{$mydata.remark}" /></div>
		</div>
		
		
		<div class="list-box">
		<div class="list-title">
			<div class="list-t-l w150px">会计科目</div>
			<div class="list-t-l w80px">客户</div>
			<div class="list-t-l w80px">供应商</div>
			<div class="list-t-l w80px">币别</div>
			<div class="list-t-l w60px">汇率</div>
			<div class="list-t-l w80px">原币借方</div>
			<div class="list-t-l w80px">借方金额</div>
			<div class="list-t-l w80px">原币贷方</div>
			<div class="list-t-l w80px">贷方金额</div>
		</div>
		<input type="hidden" id="item_num" name="item_num" value="{$item_num}" style="display:none;" />
		<div id="vou_list_body">
		<php>$total_dr=0;$total_cr=0;</php>
		<volist name="mydata_item" id="mydata_item" key="k">
		<div id="datarow{$k-1}" class="list-body">
			<div class="list-t-l w150px">
				<input type="text" id="vouitem[{$k-1}][acc_code]" name="vouitem[{$k-1}][acc_code]" value="{$mydata_item.acc_code}" style="display:none;" />
				<span class="searchbox input2"  alt="{$mydata_item.acc_code} {$mydata_item.acc_title}" title="{$mydata_item.acc_code} {$mydata_item.acc_title}">
				<input id="vouitem[{$k-1}][acc_title]" name="vouitem[{$k-1}][acc_title]" value="{$mydata_item.acc_code} {$mydata_item.acc_title}" readonly="true" type="text" class="input0" style="width:130px; " />
				<span><span class="searchbox-button" onclick="open_facc_window({$k-1})"></span></span></span>
			</div>
			<div class="list-t-l w80px">
				<input type="text" id="vouitem[{$k-1}][customer_id]" name="vouitem[{$k-1}][customer_id]" value="{$mydata_item.customer_id}" style="display:none;" />
				<span class="searchbox input2"  alt="{$mydata_item.customer_title}" title="{$mydata_item.customer_title}">
				<input id="vouitem[{$k-1}][customer_title]" name="vouitem[{$k-1}][customer_title]" value="{$mydata_item.customer_title}" readonly="true" type="text" class="input0" style="width:60px; " />
				<span><span class="searchbox-button" onclick="open_customer_window({$k-1})"></span></span></span>
			</div>
			<div class="list-t-l w80px">
				<input type="text" id="vouitem[{$k-1}][supplier_id]" name="vouitem[{$k-1}][supplier_id]" value="{$mydata_item.supplier_id}" style="display:none;" />
				<span class="searchbox input2"  alt="{$mydata_item.supplier_title}" title="{$mydata_item.supplier_title}">
				<input id="vouitem[{$k-1}][supplier_title]" name="vouitem[{$k-1}][supplier_title]" value="{$mydata_item.supplier_title}" readonly="true" type="text" class="input0" style="width:60px; " />
				<span><span class="searchbox-button" onclick="open_supplier_window({$k-1})"></span></span></span>
			</div>
			<div class="list-t-l w80px">				
				<span class="searchbox input2" >
				<input id="vouitem[{$k-1}][curr_code]" name="vouitem[{$k-1}][curr_code]" value="{$mydata_item.curr_code}" readonly="true" type="text" class="input0" style="width: 50px; ">
				<span><span class="searchbox-button" onclick="open_curr_window({$k-1})"></span></span></span>
			</div>
			<div class="list-t-l w60px"><input type="text" onchange="calc({$k-1})" id="vouitem[{$k-1}][curr_rate]" name="vouitem[{$k-1}][curr_rate]" value="{$mydata_item.curr_rate}" class="input2" readonly="true" style="width:60px;" /></div>
			<div class="list-t-l w80px"><input type="text" onchange="calc({$k-1})" onkeyup="chk_amount_input('dr',{$k-1})" id="vouitem[{$k-1}][curr_dr]" name="vouitem[{$k-1}][curr_dr]" value="{$mydata_item.curr_dr|get_round=###,6,false}" class="input2" style="width:80px;" /></div>
			<div class="list-t-l w80px"><input type="text" onchange="calc({$k-1})" onkeyup="chk_amount_input('dr',{$k-1})" id="vouitem[{$k-1}][dr]" name="vouitem[{$k-1}][dr]" value="{$mydata_item.dr|get_round=###,6,false}" class="input2" style="width:80px;"  /></div>
			<div class="list-t-l w80px"><input type="text" onchange="calc({$k-1})" onkeyup="chk_amount_input('cr',{$k-1})" id="vouitem[{$k-1}][curr_cr]" name="vouitem[{$k-1}][curr_cr]" value="{$mydata_item.curr_cr|get_round=###,6,false}" class="input2" style="width:80px;" /></div>
			<div class="list-t-l w80px"><input type="text" onchange="calc({$k-1})" onkeyup="chk_amount_input('cr',{$k-1})" id="vouitem[{$k-1}][cr]" name="vouitem[{$k-1}][cr]" value="{$mydata_item.cr|get_round=###,6,false}" class="input2" style="width:77px;"  /></div>
			<if condition="$mydata['status_flg'] eq 'A'">
			<div class="list-t-l w60px"><a href="javascript:;" onclick="datarow_remove({$k-1})">删除行{$k}</a></div>
			</if>
		</div>
		<php>$total_dr+=(double)$mydata_item['dr'];$total_cr+=(double)$mydata_item['cr'];</php>
		</volist>
		</div>
		
		</div>
		
		<div style="height:3px; clear:both;"></div>	
		
		<div class="list-title">
			<div class="list-t-l w150px">
				<a href="javascript:void(0)" id="mb3" class="easyui-menubutton" menu="#menu_appendRow" iconCls="icon-add">增加行</a>
				<div id="menu_appendRow" style="width:150px;">
					<div onclick="addRow(1)">增加一行</div>
					<div class="menu-sep"></div>
					<div onclick="addRow(3)">增加三行</div>
					<div onclick="addRow(5)">增加五行</div>
					<div onclick="addRow(10)">增加十行</div>
				</div>
			</div>
			<div class="list-t-l w60px"></div>
			<div class="list-t-l w60px"></div>
			<div class="list-t-l w80px"></div>
			<div class="list-t-l w60px"></div>
			<div class="list-t-l w80px" style="text-align:center;">借方总计</div>
			<div class="list-t-l w100px"><input type="text" id="dr_total" name="dr_total" class="input2" value="<php>echo get_round($total_dr);</php>" readonly="true" style="width:97px;"  /></div>
			<div class="list-t-l w80px" style="text-align:center;">贷方总计</div>
			<div class="list-t-l w100px"><input type="text" id="cr_total" name="cr_total" class="input2" value="<php>echo get_round($total_cr);</php>" readonly="true" style="width:97px;"  /></div>
		</div>
		
		<div style="height:3px; clear:both;"></div>		
	
		<div style="clear:both;">
				<div class="list-t-l w60px">制单：</div>
				<div class="list-t-l w200px"><input type="text" name="tmp_title" class="input" value="{$mydata.creater_username}" readonly="true" /></div>
				<div class="list-t-l w100px" align="right">过账：</div>
				<div class="list-t-l w200px"><input type="text" name="tmp_title" class="input" value="{$mydata.poster_username}" readonly="true" /></div>
		</div>
		
		<div style="clear:both;">&nbsp;</div>
	
		<div style="clear:both; display:none;">
				<div class="list-t-l w60px">&nbsp;</div>
				<div>
					<input type="hidden" id="id" name="id" class="input" value="{$mydata.id}" />
					<input type="hidden" id="refcode" name="refcode" class="input" value="{$mydata.refcode}" />
					<input type="hidden" id="refid" name="refid" class="input" value="{$mydata.refid}" />
					<input type="hidden" id="createtime" name="createtime" class="input" value="{$mydata.createtime}" />
					<input type="hidden" id="creater" name="creater" class="input" value="{$mydata.creater}" />
					<if condition="$mydata['status_flg'] eq 'A'">
					<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-save" onclick="chkfrm('save')">保存</a>
					<a href="javascript:void(0)" id="mb1" class="easyui-menubutton" menu="#menu_post_todo" iconCls="icon-ok" plain="false">过账</a>
					<div id="menu_post_todo" style="width:150px;">
						<div onclick="chkfrm('post')">过账</div>
						<div class="menu-sep"></div>
						<div onclick="chkfrm('save-post')">保存并过账</div>
					</div>
					<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-cancel" onclick="cancel_todo()">取消</a>
					</if>
					<if condition="$mydata['status_flg'] eq 'P'">
					<input type="text" id="fadate_write_off" name="fadate_write_off" class="easyui-datebox input" readonly="true" value="{$fadate_write_off}" />
					<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-no" onclick="writeoff_todo()">生成红字</a>
					</if>
					<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-print">打印</a>
					<a href="{:U('Finance/vou_list')}" class="easyui-linkbutton" iconCls="icon-undo">返回</a>
				</div>
		</div>
	</div>
	</form>
	<include file="Public:window_popup" />
	
</body>
</html>
